<?php

    class CicpAsciiImage implements Cicp_Handler_Interface
{
    function initGraph($x, $y, $z)
    {
        $graph = new Cicp_Graph();
        $graph->setPosition($x, $y, $z);

        $this->createSelector($graph, './CicpAsciiImage/');

        return $graph;
    }

    function createSelector(Cicp_Graph $graph, $dir)
    {
        if ($dh = opendir($dir)) {
            $menu = new Cicp_Graph_Menu('imageSelector');
            $menu->addButton('Delete', Cicp_Graph_Menu::BUTTON_DELETE);
            while (($file = readdir($dh)) !== false) {
                $menu->addButton($file);
            }
            $node = new Cicp_Graph_Node;
            $node->setPosition(0, 0, 0);
            $node->setShape('sph');
            $node->setTitle('Select which image to display');
            $graph->addMenu($menu);
            $node->assignMenu($menu);
            $graph->registerNode($node);
            closedir($dh);
        }
    }

    function createAsciiNodesFromImage(Cicp_Graph $graph, $image)
    {
        $data = getimagesize($image);

        switch($data['mime']) {
            case 'image/png' : $img= imagecreatefrompng($image);
                         break;
            case 'image/jpeg': $img = imagecreatefromjpeg($image);
                         break;
            case 'image/gif' : $tImg = imagecreatefromgif($image);
                         $img = imagecreatetruecolor($data[0],$data[1]);
                         imagecopy($img, $tImg, 0, 0, 0, 0, 
                                   $data[0], $data[1]);
                         imagedestroy($tImg);
                         break;
            default: break;
        } 
        $width = $data[0];
        $height = $data[1];

        if ($width * $height > 5000) {
            // too much pixels,
            // resize image
            $t = ($width * $height - (sqrt(4 * 5000 * $width * $height) / 2)) 
                / $height;

            $newWidth = intval($width - $t);
            $newHeight = intval(($height/$width) * $newWidth);

            // copy image resized
            $newImg = imagecreatetruecolor($newWidth, $newHeight);

            imagecolortransparent($newImg, 
                                  imagecolorallocate($newImg, 0, 0, 0));
            imagealphablending($newImg, false);
            imagesavealpha($newImg, true);
                
            imagecopyresampled($newImg, $img, 0, 0, 0, 0,
                               $newWidth, $newHeight, $width, $height);

            // overwrite image
            imagedestroy($img);
            $img =& $newImg;
            $height = $newHeight;
            $width = $newWidth;
        }

        $colors = array();
        for ($h = 0; $h < $height; ++$h) {
            for ($w = 0; $w < $width; ++$w) {
                $colors[] = imagecolorat($img, $w, $h);
            }
        }
        
        if (isset($_GET['outputimage'])) {

        }

        imagedestroy($img);

        // create a pixel node sample
        $size = 1;
        $pixelNode = new Cicp_Graph_Node;
        $pixelNode->setDimensions($size / 2, $size, 0.);
        $pixelNode->setColor(0, 0, 0);
        $pixelNode->setShape('rec');
        $pixelNode->setIndex(0, 0, 0);

        $menu = new Cicp_Graph_Menu('_');
        $menu->addButton('Delete Graph', Cicp_Graph_Menu::BUTTON_DELETE);
        $menu->addButton('Change', Cicp_Graph_Menu::BUTTON_HL_GROUP);
        $menu->addButton('Back', Cicp_Graph_Menu::BUTTON_HL_RESET);
        $menu->addButton('Select');
        $graph->addMenu($menu);
     
        $pixelNode->assignMenu($menu);

        $posX = 1;
        $posZ = $height / 2;

        foreach ($colors as $c) {
            $a = ($c & 0x7F000000) >> 24;
            $r = ($c & 0xFF0000) >> 16;
            $g = ($c & 0x00FF00) >> 8;
            $b = ($c & 0x0000FF);
            
            $node = clone $pixelNode;
            $node->setColor($r, $g, $b);
            $node->setAlpha(100 - ($a / 127 * 100));
            $node->setPosition($posX * $size, 0, $posZ * $size);
            $graph->registerNode($node);

            if ($posX == $width) {
                $posX = 1;
                --$posZ;
            }
            else {
                ++$posX;
            }
        }

        return $graph;
    }

    function menuChoice(Cicp_Graph $graph, Cicp_Graph_Node $node, Cicp_Graph_Menu $menu, $menuButtonLabel)
    {
        switch ($menuButtonLabel) {
        case 'Select': 
            $this->clear($graph);
            return $this->createSelector($graph, './CicpAsciiImage/');
            break;
        }
        $this->clear($graph);
        return $this->createAsciiNodesFromImage($graph, './CicpAsciiImage/' . $menuButtonLabel);
    }


    function clear($graph)
    {
        $nodes = $graph->getNodes();
        foreach ($nodes as $node) {
            $graph->removeNode($node);
        }
    }

}